package net.demo.demo4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TcpServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(12345);
        System.out.println("服务器已启动，等待客户端连接...");
        ThreadPoolExecutor pool=new ThreadPoolExecutor(
                10,
                16,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                new ThreadPoolExecutor.AbortPolicy()
        );
        while (true) {
            Socket socket = ss.accept();
            System.out.println("客户端 " + socket.getInetAddress().getHostAddress() + " 已连接");

            // 启动一个新线程处理客户端的请求
            pool.submit(new ClientHandler(socket));

        }
    }

    // 客户端处理线程
    static class ClientHandler implements Runnable {
        private final Socket socket;
        private BufferedReader reader;
        private OutputStream writer;

        public ClientHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    writer = socket.getOutputStream();

                    String clientMessage;
                    while ((clientMessage = reader.readLine()) != null) {
                        System.out.println("客户端: " + clientMessage);

                        // 检测客户端请求断开连接
                        if ("886".equals(clientMessage)) {
                            System.out.println("客户端请求断开连接");
                            writer.write("断开连接成功".getBytes());
                            writer.flush();
                            break; // 跳出内层循环，终止与该客户端的通信
                        }

                        // 从控制台读取回复
                        String reply = readConsoleInput();
                        writer.write(reply.getBytes());
                        writer.flush();
                    }
                }
            } catch (IOException e) {
                System.err.println("客户端连接出现异常: " + e.getMessage());
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                    if (writer != null) {
                        writer.close();
                    }
                    if (socket != null) {
                        socket.close();
                    }
                    System.out.println("断开成功");
                } catch (IOException e) {
                    System.err.println("关闭连接出现异常: " + e.getMessage());
                }
            }
        }


        private String readConsoleInput() throws IOException {
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("回复客户端: ");
            return consoleReader.readLine();
        }
    }
}
